const api = require("../../server/Api.js");
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: {},
    refundOrder: {},
    id: "",
    sid: "",
    allow_tk: false,
    allow_th: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    if (options.sid) {
      that.setData({
        sid: options.sid
      })
      api.refundDetail2(that.data.sid).then(res => {
        that.setData({
          refundOrder: res.refundOrder,
          id: res.refundOrder.subId
        })
        that.getOrderInfo()
      }).catch(msg => {
        let errorMsg = typeof msg == "string" ? msg : msg.message
        console.log(errorMsg)
        tt.navigateBack()
        app.showToast("获取售后信息失败!")
      })
    } else if (options.id) {
      that.setData({
        id: options.id
      })
      that.getOrderInfo()
    }
  },
  getOrderInfo() {
    let that = this
    api.applyAfterSaleInfo(that.data.id).then(res => {
      console.log(res);
      res.image = app.resetImageUrl(res.image, 172, 172);
      let goods = {
        "goodsName": res.productTitle,
        "specName": res.specCn,
        "goodsCount": res.qty,
        "goodsMaxCount": res.applyMaxQty,
        "goodsImage": res.image
      }
      if ((that.data.sid != "" || res.serviceType === 1) && (res.orderStatus === 11 || res.orderStatus === 10)) {
        that.setData({
          allow_th: true
        })
      }
      if ((that.data.sid != "" || res.serviceType === 1) && (res.orderStatus >= 4 && res.orderStatus <= 11 && res.orderStatus != 9)) {
        that.setData({
          allow_tk: true
        })
      }
      that.setData({
        goods: goods
      })
    }).catch(msg => {
      let errorMsg = typeof msg == "string" ? msg : msg.message
      console.log(errorMsg)
      tt.navigateBack()
      app.showToast("获取商品信息失败!")
    })
  },
  toSaleAfter(e) {
    tt.redirectTo({
      url: '../after-sales/after-sales?id=' + this.data.id + '&type=' + e.currentTarget.dataset.type + '&sid=' + this.data.sid
    })
  }
})